package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.Commodity;
import com.yc.supermarket.mapper.CommodityMapper;
import com.yc.supermarket.service.CommodityService;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommodityServiceImpl implements CommodityService {

    private CommodityMapper mapper;

    @Override
    public Map<String, Object> findAll() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(CommodityMapper.class);
        List<Commodity> list = mapper.findAll();
        //创建hashmap存储数据
        Map<String, Object> m = new HashMap<>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public Map<String, Object> findByTrem(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(CommodityMapper.class);
        List<Commodity> list = mapper.findByTrem(map);
        //创建hashmap存储数据
        Map<String, Object> m = new HashMap<>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public int findTotal() {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(CommodityMapper.class);
        int total = mapper.findTotal();
        MybatisUtil.close(session);
        return total;
    }

    @Override
    public int findTotalByTrem(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(CommodityMapper.class);
        int total = mapper.findTotalByTrem(map);
        MybatisUtil.close(session);
        return total;
    }

    @Override
    public int updatePromo(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(CommodityMapper.class);
        int result = mapper.updatePromo(map);
        MybatisUtil.close(session);
        return result;
    }

    @Override
    public int deleteCommodityById(String id) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(CommodityMapper.class);
        int result = mapper.deleteCommodityById(id);
        MybatisUtil.close(session);
        return result;
    }

    @Override
    public boolean addCommodity(Commodity commodity) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(CommodityMapper.class);
        boolean result = mapper.addCommodity(commodity);
        return result;
    }

    @Override
    public boolean modifyCommodity(Commodity commodity) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(CommodityMapper.class);
        boolean result = mapper.modifyCommodity(commodity);
        return result;
    }

    @Override
    public boolean updateStatus(Commodity commodity) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(CommodityMapper.class);
        boolean result = mapper.updateStatus(commodity);
        return result;
    }
}
